/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.account.Key.DomainBy;
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.common.util.ZimbraHttpConnectionManager;
/*     */ import com.zimbra.cs.account.soap.SoapProvisioning;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.OutputStream;
/*     */ import java.io.PrintStream;
/*     */ import java.net.ServerSocket;
/*     */ import java.net.Socket;
/*     */ import java.net.SocketTimeoutException;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.StringTokenizer;
/*     */ import org.apache.commons.httpclient.Header;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.HttpMethod;
/*     */ import org.apache.commons.httpclient.StatusLine;
/*     */ import org.apache.commons.httpclient.methods.GetMethod;
/*     */ import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
/*     */ import org.apache.commons.httpclient.methods.PostMethod;
/*     */ import org.apache.commons.httpclient.params.HttpClientParams;
/*     */ import org.apache.commons.httpclient.params.HttpMethodParams;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestZimbraHttpConnectionManager
/*     */ {
/*     */   @BeforeClass
/*     */   public static void init()
/*     */     throws Exception
/*     */   {
/*  55 */     CliUtil.toolSetup("INFO");
/*     */   }
/*     */   
/*     */   public static void dumpResponse(int respCode, HttpMethod method, String prefix)
/*     */     throws IOException
/*     */   {
/*  61 */     prefix = prefix + " - ";
/*     */     
/*     */ 
/*  64 */     int statusCode = method.getStatusCode();
/*  65 */     String statusLine = method.getStatusLine().toString();
/*     */     
/*  67 */     System.out.println(prefix + "respCode=" + respCode);
/*  68 */     System.out.println(prefix + "statusCode=" + statusCode);
/*  69 */     System.out.println(prefix + "statusLine=" + statusLine);
/*     */     
/*     */ 
/*  72 */     System.out.println(prefix + "Headers");
/*  73 */     Header[] respHeaders = method.getResponseHeaders();
/*  74 */     for (int i = 0; i < respHeaders.length; i++) {
/*  75 */       String header = respHeaders[i].toString();
/*     */       
/*  77 */       System.out.println(prefix + header.trim());
/*     */     }
/*     */     
/*     */ 
/*  81 */     byte[] bytes = ByteUtil.getContent(method.getResponseBodyAsStream(), 0);
/*  82 */     System.out.println(prefix + bytes.length + " bytes read");
/*  83 */     System.out.println(new String(bytes));
/*     */   }
/*     */   
/*     */ 
/*     */   private static class TestGetThread
/*     */     extends Thread
/*     */   {
/*     */     private HttpClient mHttpClient;
/*     */     
/*     */     private GetMethod mMethod;
/*     */     private int mId;
/*     */     
/*     */     public TestGetThread(HttpClient httpClient, GetMethod method, int id)
/*     */     {
/*  97 */       this.mHttpClient = httpClient;
/*  98 */       this.mMethod = method;
/*  99 */       this.mId = id;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */     public void run()
/*     */     {
/* 106 */       long startTime = System.currentTimeMillis();
/*     */       
/*     */       try
/*     */       {
/* 110 */         System.out.println(this.mId + " - about to get something from " + this.mMethod.getURI());
/*     */         
/* 112 */         int respCode = HttpClientUtil.executeMethod(this.mHttpClient, this.mMethod);
/*     */         
/* 114 */         System.out.println(this.mId + " - get executed");
/*     */       }
/*     */       catch (Exception e)
/*     */       {
/*     */         long elapsedTime;
/*     */         
/* 120 */         System.out.println(this.mId + " - error: " + e);
/* 121 */         e.printStackTrace();
/*     */       } finally {
/*     */         long elapsedTime;
/* 124 */         long endTime = System.currentTimeMillis();
/* 125 */         long elapsedTime = endTime - startTime;
/* 126 */         System.out.println("Finished, elapsedTime=" + elapsedTime + " milli seconds");
/*     */         
/*     */ 
/* 129 */         this.mMethod.releaseConnection();
/* 130 */         System.out.println(this.mId + " - connection released");
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testReaper()
/*     */     throws Exception
/*     */   {
/* 147 */     String[] urisToGet = { "http://localhost:7070/zimbra/public/empty.html", "http://localhost:7071/service/admin/soap" };
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 158 */     ZimbraHttpConnectionManager connMgr = ZimbraHttpConnectionManager.getExternalHttpConnMgr();
/*     */     
/*     */ 
/* 161 */     TestGetThread[] threads = new TestGetThread[urisToGet.length];
/* 162 */     for (int i = 0; i < threads.length; i++) {
/* 163 */       GetMethod get = new GetMethod(urisToGet[i]);
/* 164 */       get.setFollowRedirects(true);
/* 165 */       threads[i] = new TestGetThread(connMgr.newHttpClient(), get, i + 1);
/*     */     }
/*     */     
/* 168 */     ZimbraHttpConnectionManager.startReaperThread();
/*     */     
/*     */ 
/* 171 */     for (int j = 0; j < threads.length; j++) {
/* 172 */       threads[j].start();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 197 */     Thread.sleep(3600000L);
/*     */   }
/*     */   
/*     */ 
/*     */   private static class SimpleHttpServer
/*     */     implements Runnable
/*     */   {
/* 204 */     private static Thread mServerThread = null;
/* 205 */     private static SimpleHttpServer sServer = null;
/*     */     
/*     */     private int mPort;
/*     */     
/*     */     private ServerSocket mServerSocket;
/* 210 */     private boolean mShutdownRequested = false;
/*     */     
/*     */     private static enum DelayWhen {
/* 213 */       BEFORE_READING_REQ_LINE, 
/* 214 */       BEFORE_READING_HEADERS, 
/* 215 */       GET_BEFORE_FETCHING_RESOURCE, 
/* 216 */       POST_BEFORE_READING_BODY, 
/* 217 */       BEFORE_WRITING_RESPONSE_HEADERS, 
/* 218 */       BEFORE_WRITING_RESPONSE_BODY, 
/* 219 */       DURING_WRITING_RESPONSE_BODY, 
/* 220 */       AFTER_WRITING_RESPONSE_BODY;
/*     */       
/*     */       private DelayWhen() {} }
/*     */     
/* 224 */     private static synchronized void start(int port) { if (mServerThread != null) {
/* 225 */         log("start server: server already started");
/* 226 */         return;
/*     */       }
/*     */       
/* 229 */       log("starting server");
/* 230 */       sServer = new SimpleHttpServer(port);
/* 231 */       mServerThread = new Thread(sServer);
/* 232 */       mServerThread.start();
/*     */     }
/*     */     
/*     */     private static synchronized void shutdown() {
/* 236 */       if (mServerThread == null) {
/* 237 */         log("shutdown server: server is not running");
/* 238 */         return;
/*     */       }
/*     */       
/* 241 */       sServer.requestShutdown();
/*     */       
/* 243 */       mServerThread = null;
/*     */     }
/*     */     
/*     */     private SimpleHttpServer(int port) {
/* 247 */       this.mPort = port;
/*     */     }
/*     */     
/*     */     private synchronized void requestShutdown() {
/* 251 */       this.mShutdownRequested = true;
/*     */       try {
/* 253 */         this.mServerSocket.close();
/*     */       }
/*     */       catch (IOException e) {}
/*     */     }
/*     */     
/*     */     private static void log(String msg)
/*     */     {
/* 260 */       System.out.println("*** SimpleHttpServer: " + msg);
/*     */     }
/*     */     
/*     */     public void run()
/*     */     {
/*     */       try {
/* 266 */         this.mServerSocket = new ServerSocket(this.mPort);
/* 267 */         log("started on port " + this.mServerSocket.getLocalPort());
/*     */         
/*     */ 
/* 270 */         while (!this.mShutdownRequested) {
/* 271 */           Socket socket = this.mServerSocket.accept();
/* 272 */           log("new connection accepted " + socket.getInetAddress() + ":" + socket.getPort());
/*     */           
/*     */           try
/*     */           {
/* 276 */             TestZimbraHttpConnectionManager.HttpRequestHandler request = new TestZimbraHttpConnectionManager.HttpRequestHandler(socket, null);
/*     */             
/* 278 */             Thread thread = new Thread(request);
/*     */             
/* 280 */             thread.start();
/*     */           } catch (Exception e) {
/* 282 */             e.printStackTrace();
/*     */           }
/*     */         }
/*     */       } catch (IOException e) {
/* 286 */         if (!this.mShutdownRequested) {
/* 287 */           e.printStackTrace();
/*     */         }
/*     */       } finally {
/* 290 */         log("exiting server");
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   private static class HttpRequestHandler
/*     */     implements Runnable
/*     */   {
/*     */     static final String CRLF = "\r\n";
/*     */     
/*     */     private static final String SERVER_LINE = "Server: brain dead java httpServer\r\n";
/*     */     
/*     */     private static final String STATUS_LINE_200 = "HTTP/1.0 200 OK\r\n";
/*     */     private static final String STATUS_LINE_404 = "HTTP/1.0 404 Not Found\r\n";
/*     */     private static final String HEADER_CONTENT_LENGTH = "Content-Length";
/*     */     private static final String HEADER_CONTENT_TYPE = "Content-Type";
/*     */     Socket socket;
/*     */     InputStream input;
/*     */     OutputStream output;
/*     */     BufferedReader reader;
/*     */     private String url;
/* 312 */     private Map<String, String> headers = new HashMap();
/* 313 */     private Map<String, String> queryParams = new HashMap();
/*     */     
/*     */     private HttpRequestHandler(Socket socket) throws Exception
/*     */     {
/* 317 */       this.socket = socket;
/* 318 */       this.input = socket.getInputStream();
/* 319 */       this.output = socket.getOutputStream();
/* 320 */       this.reader = new BufferedReader(new InputStreamReader(this.input));
/*     */     }
/*     */     
/*     */     public void run()
/*     */     {
/*     */       try {
/* 326 */         processRequest();
/*     */       } catch (Exception e) {
/* 328 */         e.printStackTrace();
/*     */       }
/*     */     }
/*     */     
/* 332 */     private void getHeaders() throws Exception { delayIfRequested(TestZimbraHttpConnectionManager.SimpleHttpServer.DelayWhen.BEFORE_READING_HEADERS);
/*     */       
/*     */ 
/*     */       for (;;)
/*     */       {
/* 337 */         String header = this.reader.readLine();
/*     */         
/* 339 */         if ((header.equals("\r\n")) || (header.equals(""))) {
/*     */           break;
/*     */         }
/*     */         
/* 343 */         String[] parts = header.split(":");
/* 344 */         this.headers.put(parts[0].trim(), parts[1].trim());
/*     */       }
/*     */     }
/*     */     
/*     */     private void parseRequestLine(String req)
/*     */     {
/* 350 */       String[] parts = req.split("\\?");
/* 351 */       this.url = parts[0];
/*     */       
/* 353 */       String qp = null;
/* 354 */       long waitInServer = 0L;
/* 355 */       if (parts.length == 2) {
/* 356 */         qp = parts[1];
/*     */         
/* 358 */         String[] params = qp.split("&");
/* 359 */         for (String param : params) {
/* 360 */           String[] nameValue = param.split("=");
/* 361 */           this.queryParams.put(nameValue[0], nameValue[1]);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */     private void delayIfRequested(TestZimbraHttpConnectionManager.SimpleHttpServer.DelayWhen delayWhen) throws Exception {
/* 367 */       String delay = (String)this.queryParams.get(delayWhen.name());
/* 368 */       if (delay != null) {
/* 369 */         int delayMilliSecs = Integer.valueOf(delay).intValue();
/* 370 */         if (delayMilliSecs != 0) {
/* 371 */           TestZimbraHttpConnectionManager.SimpleHttpServer.access$100("Delaying " + delay + " milli seconds: " + delayWhen.name());
/* 372 */           Thread.sleep(delayMilliSecs);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */     private static void sendBytes(FileInputStream fis, OutputStream os) throws Exception
/*     */     {
/* 379 */       byte[] buffer = new byte['Ѐ'];
/* 380 */       int bytes = 0;
/*     */       
/*     */ 
/* 383 */       while ((bytes = fis.read(buffer)) != -1) {
/* 384 */         os.write(buffer, 0, bytes);
/*     */       }
/*     */     }
/*     */     
/*     */     private void doGet() throws Exception
/*     */     {
/* 390 */       delayIfRequested(TestZimbraHttpConnectionManager.SimpleHttpServer.DelayWhen.GET_BEFORE_FETCHING_RESOURCE);
/*     */       
/*     */ 
/* 393 */       String fileName = this.url;
/* 394 */       FileInputStream fis = null;
/* 395 */       boolean fileExists = true;
/*     */       try {
/* 397 */         fis = new FileInputStream(fileName);
/*     */       } catch (FileNotFoundException e) {
/* 399 */         fileExists = false;
/*     */       }
/*     */       
/* 402 */       String statusLine = null;
/* 403 */       String contentTypeLine = null;
/* 404 */       String entityBody = null;
/* 405 */       String contentLengthLine = null;
/* 406 */       if (fileExists) {
/* 407 */         statusLine = "HTTP/1.0 200 OK\r\n";
/* 408 */         contentTypeLine = "Content-Type: text/plain\r\n";
/* 409 */         contentLengthLine = "Content-Length: " + new Integer(fis.available()).toString() + "\r\n";
/*     */       }
/*     */       else {
/* 412 */         statusLine = "HTTP/1.0 404 Not Found\r\n";
/* 413 */         contentTypeLine = "Content-Type: text/html\r\n";
/* 414 */         entityBody = "<HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD><BODY>404 Not Found<br>File " + fileName + "</BODY></HTML>";
/*     */         
/*     */ 
/* 417 */         contentLengthLine = "Content-Length: " + entityBody.length() + "\r\n";
/*     */       }
/*     */       
/* 420 */       delayIfRequested(TestZimbraHttpConnectionManager.SimpleHttpServer.DelayWhen.BEFORE_WRITING_RESPONSE_HEADERS);
/*     */       
/*     */ 
/* 423 */       this.output.write(statusLine.getBytes());
/*     */       
/*     */ 
/* 426 */       this.output.write("Server: brain dead java httpServer\r\n".getBytes());
/*     */       
/*     */ 
/* 429 */       this.output.write(contentTypeLine.getBytes());
/*     */       
/*     */ 
/* 432 */       this.output.write(contentLengthLine.getBytes());
/*     */       
/*     */ 
/* 435 */       this.output.write("\r\n".getBytes());
/*     */       
/* 437 */       delayIfRequested(TestZimbraHttpConnectionManager.SimpleHttpServer.DelayWhen.BEFORE_WRITING_RESPONSE_BODY);
/*     */       
/*     */ 
/* 440 */       if (fileExists) {
/* 441 */         sendBytes(fis, this.output);
/* 442 */         fis.close();
/*     */       } else {
/* 444 */         this.output.write(entityBody.getBytes());
/*     */       }
/*     */       
/* 447 */       delayIfRequested(TestZimbraHttpConnectionManager.SimpleHttpServer.DelayWhen.AFTER_WRITING_RESPONSE_BODY);
/*     */     }
/*     */     
/*     */     private void doPost() throws Exception
/*     */     {
/* 452 */       delayIfRequested(TestZimbraHttpConnectionManager.SimpleHttpServer.DelayWhen.POST_BEFORE_READING_BODY);
/*     */       
/* 454 */       int contentLength = Integer.valueOf((String)this.headers.get("Content-Length")).intValue();
/* 455 */       int firstHalfLen = contentLength / 2;
/* 456 */       int secondHalfLen = contentLength - firstHalfLen;
/*     */       
/* 458 */       byte[] bodyFirstHalf = new byte[firstHalfLen];
/* 459 */       this.input.read(bodyFirstHalf);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 464 */       byte[] bodySecondHalf = new byte[secondHalfLen];
/* 465 */       this.input.read(bodySecondHalf);
/*     */       
/* 467 */       String entityBody = "all is well!";
/* 468 */       String contentTypeLine = "Content-Type: text/plain\r\n";
/* 469 */       String contentLengthLine = "Content-Length: " + entityBody.getBytes().length + "\r\n";
/*     */       
/* 471 */       delayIfRequested(TestZimbraHttpConnectionManager.SimpleHttpServer.DelayWhen.BEFORE_WRITING_RESPONSE_HEADERS);
/*     */       
/*     */ 
/* 474 */       this.output.write("HTTP/1.0 200 OK\r\n".getBytes());
/*     */       
/*     */ 
/* 477 */       this.output.write("Server: brain dead java httpServer\r\n".getBytes());
/*     */       
/*     */ 
/* 480 */       this.output.write(contentTypeLine.getBytes());
/*     */       
/*     */ 
/* 483 */       this.output.write(contentLengthLine.getBytes());
/*     */       
/*     */ 
/* 486 */       this.output.write("\r\n".getBytes());
/*     */       
/* 488 */       delayIfRequested(TestZimbraHttpConnectionManager.SimpleHttpServer.DelayWhen.BEFORE_WRITING_RESPONSE_BODY);
/*     */       
/*     */ 
/* 491 */       this.output.write(entityBody.getBytes());
/*     */       
/* 493 */       delayIfRequested(TestZimbraHttpConnectionManager.SimpleHttpServer.DelayWhen.AFTER_WRITING_RESPONSE_BODY);
/*     */     }
/*     */     
/*     */     private void processRequest()
/*     */       throws Exception
/*     */     {
/* 499 */       delayIfRequested(TestZimbraHttpConnectionManager.SimpleHttpServer.DelayWhen.BEFORE_READING_REQ_LINE);
/* 500 */       String reqLine = this.reader.readLine();
/* 501 */       TestZimbraHttpConnectionManager.SimpleHttpServer.access$100(reqLine);
/* 502 */       if ((!reqLine.equals("\r\n")) && (!reqLine.equals("")))
/*     */       {
/*     */ 
/*     */ 
/* 506 */         getHeaders();
/*     */         
/* 508 */         StringTokenizer s = new StringTokenizer(reqLine);
/* 509 */         String method = s.nextToken();
/*     */         
/* 511 */         String req = s.nextToken();
/* 512 */         parseRequestLine(req);
/*     */         
/* 514 */         if (method.equals("GET")) {
/* 515 */           doGet();
/* 516 */         } else if (method.equals("POST")) {
/* 517 */           doPost();
/*     */         }
/*     */       }
/*     */       
/*     */ 
/*     */ 
/*     */       try
/*     */       {
/* 525 */         this.output.close();
/* 526 */         this.reader.close();
/* 527 */         this.socket.close();
/*     */       }
/*     */       catch (Exception e) {}
/*     */     }
/*     */   }
/*     */   
/*     */   public void testSoTimeoutViaHttpMethod()
/*     */     throws Exception
/*     */   {
/* 536 */     int serverPort = 7778;
/* 537 */     String resourceToGet = "/opt/zimbra/unittest/rights-unittest.xml";
/* 538 */     long delayInServer = 10000L;
/* 539 */     int soTimeout = 3000;
/*     */     
/* 541 */     String qp = "?" + TestZimbraHttpConnectionManager.SimpleHttpServer.DelayWhen.BEFORE_WRITING_RESPONSE_HEADERS.name() + "=" + delayInServer;
/* 542 */     String uri = "http://localhost:" + serverPort + resourceToGet + qp;
/*     */     
/*     */ 
/* 545 */     SimpleHttpServer.start(serverPort);
/*     */     
/*     */ 
/* 548 */     HttpClient httpClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/*     */     
/* 550 */     GetMethod method = new GetMethod(uri);
/*     */     
/*     */ 
/* 553 */     method.getParams().setSoTimeout(soTimeout);
/*     */     
/* 555 */     long startTime = System.currentTimeMillis();
/*     */     
/*     */     try
/*     */     {
/* 559 */       int respCode = httpClient.executeMethod(method);
/*     */       
/* 561 */       dumpResponse(respCode, method, "");
/* 562 */       Assert.fail();
/*     */     }
/*     */     catch (SocketTimeoutException e) {
/* 565 */       long endTime = System.currentTimeMillis();
/* 566 */       long elapsedTime = endTime - startTime;
/* 567 */       System.out.println("Client timed out after " + elapsedTime + " msecs");
/*     */     } catch (Exception e) {
/* 569 */       e.printStackTrace();
/* 570 */       Assert.fail();
/*     */     } finally {
/* 572 */       method.releaseConnection();
/*     */     }
/*     */     
/*     */ 
/* 576 */     SimpleHttpServer.access$300();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testSoTimeoutViaConnMgrParam()
/*     */     throws Exception
/*     */   {
/* 587 */     int serverPort = 7778;
/* 588 */     String resourceToGet = "/opt/zimbra/unittest/rights-unittest.xml";
/* 589 */     long delayInServer = 10000L;
/* 590 */     int soTimeout = 3000;
/*     */     
/* 592 */     String qp = "?" + TestZimbraHttpConnectionManager.SimpleHttpServer.DelayWhen.BEFORE_WRITING_RESPONSE_HEADERS.name() + "=" + delayInServer;
/* 593 */     String uri = "http://localhost:" + serverPort + resourceToGet + qp;
/*     */     
/*     */ 
/* 596 */     SimpleHttpServer.start(serverPort);
/*     */     
/* 598 */     HttpClient httpClient = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient();
/*     */     
/* 600 */     GetMethod method = new GetMethod(uri);
/*     */     
/* 602 */     long startTime = System.currentTimeMillis();
/*     */     
/*     */     try
/*     */     {
/* 606 */       int respCode = httpClient.executeMethod(method);
/*     */       
/* 608 */       dumpResponse(respCode, method, "");
/* 609 */       Assert.fail();
/*     */     }
/*     */     catch (SocketTimeoutException e) {
/* 612 */       long endTime = System.currentTimeMillis();
/* 613 */       long elapsedTime = endTime - startTime;
/* 614 */       System.out.println("Client timed out after " + elapsedTime + " msecs");
/*     */     } catch (Exception e) {
/* 616 */       e.printStackTrace();
/* 617 */       Assert.fail();
/*     */     } finally {
/* 619 */       method.releaseConnection();
/*     */     }
/*     */     
/*     */ 
/* 623 */     SimpleHttpServer.access$300();
/*     */   }
/*     */   
/*     */   public void testSoTimeoutViaHttpPostMethod()
/*     */     throws Exception
/*     */   {
/* 629 */     int serverPort = 7778;
/* 630 */     String resourceToPost = "/opt/zimbra/unittest/rights-unittest.xml";
/* 631 */     long delayInServer = 100000L;
/* 632 */     int soTimeout = 60000;
/*     */     
/* 634 */     String qp = "?" + TestZimbraHttpConnectionManager.SimpleHttpServer.DelayWhen.BEFORE_WRITING_RESPONSE_HEADERS.name() + "=" + delayInServer;
/* 635 */     String uri = "http://localhost:" + serverPort + resourceToPost + qp;
/*     */     
/*     */ 
/* 638 */     SimpleHttpServer.start(serverPort);
/*     */     
/*     */ 
/* 641 */     HttpClient httpClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/* 642 */     PostMethod method = new PostMethod(uri);
/* 643 */     method.getParams().setSoTimeout(soTimeout);
/*     */     
/* 645 */     File file = new File(resourceToPost);
/* 646 */     FileInputStream fis = null;
/*     */     
/* 648 */     long startTime = System.currentTimeMillis();
/*     */     try
/*     */     {
/* 651 */       fis = new FileInputStream(file);
/* 652 */       InputStreamRequestEntity isre = new InputStreamRequestEntity(fis, file.length(), "application/octet-stream");
/*     */       
/* 654 */       method.setRequestEntity(isre);
/* 655 */       int respCode = httpClient.executeMethod(method);
/*     */       
/* 657 */       dumpResponse(respCode, method, "");
/* 658 */       Assert.fail();
/*     */     } catch (SocketTimeoutException e) {
/* 660 */       e.printStackTrace();
/*     */       
/* 662 */       long endTime = System.currentTimeMillis();
/* 663 */       long elapsedTime = endTime - startTime;
/* 664 */       System.out.println("Client timed out after " + elapsedTime + " msecs");
/*     */     } catch (Exception e) {
/* 666 */       e.printStackTrace();
/* 667 */       Assert.fail();
/*     */     } finally {
/* 669 */       method.releaseConnection();
/*     */     }
/*     */     
/*     */ 
/* 673 */     SimpleHttpServer.access$300();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testHttpClientConnectionManagerTimeout()
/*     */     throws Exception
/*     */   {
/* 686 */     int serverPort = 7778;
/* 687 */     String path = "/Users/pshao/p4/main/ZimbraServer/src/java/com/zimbra/qa/unittest/TestZimbraHttpConnectionManager.java";
/* 688 */     long delayInServer = 10000L;
/* 689 */     String qp = "?" + TestZimbraHttpConnectionManager.SimpleHttpServer.DelayWhen.GET_BEFORE_FETCHING_RESOURCE.name() + "=" + delayInServer;
/*     */     
/*     */ 
/* 692 */     SimpleHttpServer.start(serverPort);
/*     */     
/* 694 */     ZimbraHttpConnectionManager connMgr = ZimbraHttpConnectionManager.getExternalHttpConnMgr();
/*     */     
/*     */ 
/* 697 */     GetMethod method1 = new GetMethod("http://localhost:" + serverPort + path + qp);
/* 698 */     TestGetThread thread1 = new TestGetThread(connMgr.newHttpClient(), method1, 1);
/* 699 */     thread1.start();
/*     */     
/* 701 */     Thread.sleep(1000L);
/*     */     
/*     */ 
/* 704 */     GetMethod method2 = new GetMethod("http://localhost:" + serverPort + path + qp);
/* 705 */     TestGetThread thread2 = new TestGetThread(connMgr.newHttpClient(), method1, 2);
/* 706 */     thread2.start();
/*     */     
/*     */ 
/* 709 */     Thread.sleep(60000L);
/*     */     
/*     */ 
/* 712 */     SimpleHttpServer.access$300();
/*     */   }
/*     */   
/*     */   private static void runSoapProv(String msg) {
/* 716 */     System.out.println(msg);
/* 717 */     SoapProvisioning sp = new SoapProvisioning();
/* 718 */     String uri = LC.zimbra_admin_service_scheme.value() + LC.zimbra_zmprov_default_soap_server.value() + ":" + LC.zimbra_admin_service_port.intValue() + "/service/admin/soap/";
/*     */     
/*     */ 
/*     */ 
/* 722 */     sp.soapSetURI(uri);
/*     */     try {
/* 724 */       sp.getDomainInfo(Key.DomainBy.name, "phoebe.mac");
/*     */     } catch (ServiceException e) {
/* 726 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */   
/*     */   private static class SoapProvThread extends Thread
/*     */   {
/*     */     private String mId;
/*     */     
/*     */     public SoapProvThread(String id)
/*     */     {
/* 736 */       this.mId = id;
/*     */     }
/*     */     
/*     */     public void run() {
/* 740 */       TestZimbraHttpConnectionManager.runSoapProv(this.mId);
/*     */       try
/*     */       {
/* 743 */         Thread.sleep(3600000L);
/*     */       }
/*     */       catch (InterruptedException e) {}
/*     */     }
/*     */   }
/*     */   
/*     */   private void runSoapProvParallel(int num) {
/* 750 */     for (int i = 0; i < num; i++) {
/* 751 */       SoapProvThread thread = new SoapProvThread(Integer.valueOf(i).toString());
/* 752 */       thread.start();
/*     */     }
/*     */   }
/*     */   
/*     */   private void runSoapProvSerial(int num) {
/* 757 */     for (int i = 0; i < num; i++) {
/* 758 */       runSoapProv(Integer.valueOf(i).toString());
/*     */     }
/*     */   }
/*     */   
/*     */   public void testSoapProv()
/*     */     throws Exception
/*     */   {
/* 765 */     runSoapProvParallel(3);
/*     */   }
/*     */   
/*     */ 
/*     */   private void runTest(HttpClient httpClient, String id, boolean authPreemp)
/*     */   {
/* 771 */     GetMethod method = new GetMethod("http://localhost:7070/");
/*     */     
/* 773 */     long startTime = System.currentTimeMillis();
/*     */     
/*     */     try
/*     */     {
/* 777 */       System.out.println(id + " - about to get something from " + method.getURI());
/*     */       
/*     */ 
/* 780 */       if (authPreemp) {
/* 781 */         httpClient.getParams().setAuthenticationPreemptive(true);
/*     */       }
/*     */       
/* 784 */       int respCode = HttpClientUtil.executeMethod(httpClient, method);
/*     */       
/* 786 */       System.out.println(id + " - get executed");
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/*     */       long elapsedTime;
/*     */       
/* 792 */       System.out.println(id + " - error: " + e);
/* 793 */       e.printStackTrace();
/*     */     } finally {
/*     */       long elapsedTime;
/* 796 */       long endTime = System.currentTimeMillis();
/* 797 */       long elapsedTime = endTime - startTime;
/*     */       
/*     */ 
/*     */ 
/* 801 */       method.releaseConnection();
/* 802 */       System.out.println(id + " - connection released");
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public void testAuthenticationPreemptive()
/*     */     throws Exception
/*     */   {
/*     */     
/*     */     
/* 812 */     for (int i = 0; i < 10; i++)
/*     */     {
/* 814 */       runTest(ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient(), "EXT-authPreemp" + i, true);
/* 815 */       runTest(ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient(), "EXT" + i, false);
/* 816 */       runTest(ZimbraHttpConnectionManager.getInternalHttpConnMgr().getDefaultHttpClient(), "INT" + i, false);
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void junk() throws Exception
/*     */   {
/* 823 */     String uri = "http://phoebe.mbp:7070/service/soap/AuthRequest";
/*     */     
/* 825 */     HttpClient httpClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/*     */     
/* 827 */     GetMethod method = new GetMethod(uri);
/*     */     
/*     */     try
/*     */     {
/* 831 */       int respCode = httpClient.executeMethod(method);
/*     */       
/* 833 */       dumpResponse(respCode, method, "");
/* 834 */       Assert.fail();
/*     */     } catch (Exception e) {
/* 836 */       e.printStackTrace();
/* 837 */       Assert.fail();
/*     */     } finally {
/* 839 */       method.releaseConnection();
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestZimbraHttpConnectionManager.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */